package com.ymxk.livedata;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.timestar.event.EventData;
import com.timestar.event.LiveDataBus;

public class LiveData1Activity extends AppCompatActivity {

    public static void jumpTo(Context context) {
        Intent intent = new Intent(context, LiveData1Activity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_live_data1);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        findViewById(R.id.tvOpen).setOnClickListener(view -> {
            //findViewById/
            LiveData2Activity.jumpTo(this);
        });

        findViewById(R.id.tvFirst).setOnClickListener(view -> {
            LiveDataBus.getDefault().post(new EventData("5555", 7));
            finish();
        });


        LiveDataBus.getDefault().register(this, eventData -> {
            int what = eventData.what;
            Toast.makeText(this,
                    eventData.obj + "-->" + what,
                    Toast.LENGTH_SHORT).show();
        });

    }
}